package cn.kt.service.impl;

import cn.kt.common.pojo.PageObject;
import cn.kt.common.vo.ktResult;
import cn.kt.dao.TeacherDao;
import cn.kt.pojo.TeacherPojo;
import cn.kt.service.UserTeacherService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserTeacherServiceImpl implements UserTeacherService {

    @Autowired
    TeacherDao teacherDao;

    //查找全部的用户
    @Override
    public PageObject<List<TeacherPojo>> findAll(Integer id,String user_name, Integer state, Integer pageCurrent) {
        //1.参数校验 目标页码值为空或者小于1时，强制=1
        if (pageCurrent==null||pageCurrent<1)
            pageCurrent = 1;
        //设置最大显示页码数
        int pageListSize = 5;
        //2.启动分页配置(底层会启动一个mybatis拦截器，拦截sql会话)
        int pageSize=2;//每页最大显示数
        Page<List<TeacherPojo>> page = PageHelper.startPage(pageCurrent,pageSize);
        //执行sql
        List<TeacherPojo> list = teacherDao.findUser(null,user_name, state);
        return new PageObject((int)page.getTotal(),list,pageSize,pageCurrent,pageListSize);
    }

    //展示 修改人员 的数据
    @Override
    public TeacherPojo findUser(TeacherPojo tp) {
        return teacherDao.userUpdateShow(tp);
    }

    //账号名重复性验证
    @Override
    public ktResult checkUser(String account) {
        Integer n = teacherDao.checkUser(account);
        if (n.equals(1)){
            return ktResult.fail();
        }
        return ktResult.success();
    }

    //验证手机号
    @Override
    public ktResult checkPhone(Long phone) {
        Integer pInt = teacherDao.checkPhone(phone);
        System.out.println(pInt);
        if (!pInt.equals(0)){
            HashMap hm = teacherDao.findPhoneUser(phone);
            return ktResult.fail("改手机号已被用户：",hm);
        }
        return ktResult.success();
    }

    //添加用户(管理员)
    @Override
    public ktResult addUser(TeacherPojo tp) {
        tp.setState(1);//默认为在职状态
        teacherDao.addUser(tp);
        return ktResult.success();
    }

    @Override
    public ktResult updateUser(TeacherPojo tp) {
        Integer i = teacherDao.updateUser(tp);
        String msg = "修改"+i+"数据成功";
        return ktResult.success(msg);
    }
}
